package com.backweb.dao.Impl;

import com.backweb.dao.ArticleDaoCustom;
import com.backweb.model.Article;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;




public class ArticleDaoCustomImpl implements ArticleDaoCustom {


    @PersistenceContext
    private EntityManager em;
    @Override
    public List<Article> findPage(String t1, String t2, Integer pageNo, Integer rows) {

        List<Article> items=new ArrayList<>();
        try {
            int index = 0;
            if (pageNo < 1) { index = 0;   } else {   index = (pageNo - 1) * rows; }

            String sql = "SELECT * FROM  article where  rid>0  ";
            if (!t1.equals(null) && !t1.equals("")) {
                sql += " and DATE_FORMAT(addtime,'%Y-%m-%d')>='" + t1 + "'";
            }

            if (!t2.equals(null) && !t2.equals("")) {
                sql += " and DATE_FORMAT(addtime,'%Y-%m-%d')<= '" + t2 + "'";
            }

            sql += " ORDER BY addtime desc ";
            sql += "LIMIT " + index + "," + rows;
            System.out.println(sql);

            Query query = em.createNativeQuery(sql, Article.class);
            items = query.getResultList();
        }catch (Exception ex){}
        return items;
    }

    @Override
    public Integer findPageTotal(String t1, String t2) {
        Integer total=0;
        try {
            String sql = "SELECT count(*) total FROM  article where  rid>0  ";
            if (!t1.equals(null) && !t1.equals("")) {
                sql += " and DATE_FORMAT(addtime,'%Y-%m-%d')>='" + t1 + "'";
            }

            if (!t2.equals(null) && !t2.equals("")) {
                sql += " and DATE_FORMAT(addtime,'%Y-%m-%d')<= '" + t2 + "'";
            }
            Query query = em.createNativeQuery(sql);
            Object obj = query.getSingleResult();
            String temp=obj.toString();
            total = Integer.valueOf(temp);
          //  System.out.println(total);
        }catch (Exception ex)
        {}
        return total;
    }

    @Override
    public int dayTotal(int type) {
        Integer total=0;
        try {
            String sql = "select count(*) total  from article where to_days(addtime) = to_days(now())";
            switch (type) {
                case 0:
                    sql = "select count(*) total  from article where to_days(addtime) = to_days(now())";
                    break;
                case 1:
                    sql = "SELECT count(*) total  FROM article WHERE TO_DAYS( NOW( ) ) - TO_DAYS( addtime) = 1";
                    break;
                case 7:
                    sql = "SELECT count(*) total  FROM article where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(addtime)";
                    break;
            }

            Query query = em.createNativeQuery(sql);
            Object obj = query.getSingleResult();
            String temp = obj.toString();
            total = Integer.valueOf(temp);
        }catch (Exception ex){}
        return total;
    }

    @Override
    public List<Article> dayPage(int type, Integer pageNo, Integer rows) {

        List<Article> items=new ArrayList<>();
        try {
            int index = 0;
            if (pageNo < 1) {
                index = 0;
            } else {
                index = (pageNo - 1) * rows;
            }
            String sql = "select * from article where to_days(addtime) = to_days(now())";
            switch (type) {
                case 0:
                    sql = "select * from article where to_days(addtime) = to_days(now())";
                    break;
                case 1:
                    sql = "SELECT * FROM article WHERE TO_DAYS( NOW( ) ) - TO_DAYS( addtime) = 1";
                    break;
                case 7:
                    sql = "SELECT *  FROM article where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(addtime)";
                    break;
            }

            sql += " ORDER BY addtime desc ";
            sql += " LIMIT " + index + "," + rows;
            System.out.println(sql);

            Query query = em.createNativeQuery(sql, Article.class);
            items = query.getResultList();
        }catch (Exception ex){}

        return items;
    }
}
